package com.css.fxfzfxqh.modules.singleIndexRiskZoning.repository.impl;

import com.css.fxfzfxqh.modules.singleIndexRiskZoning.model.entity.FxqhAssessPdQhResult;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.param.QhSingleExportParam;
import com.css.fxfzfxqh.modules.singleIndexRiskZoning.vo.QhSingleExportVO;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: zhangSongRui
 * @CreateTime: 2023/07/25
 * @Description: TODO
 */
@Repository
public class ProbabilityAssessQhRepository {
    @Resource
    JdbcTemplate jdbcTemplate;

    @Resource
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;


    public void delByTaskId(String taskId,List<String> typeList) {
        jdbcTemplate.update("delete from fxqh_assess_pd_qh_result where task_id = ? and estimate_obj in ('"+String.join("','", typeList)+"')", taskId);
    }

    public void saveFxqhAssessPdResult(FxqhAssessPdQhResult result) {
        String sql = "INSERT INTO fxqh_assess_pd_qh_result(id, province, city, county, calculated_value, geom, fae_id, borderland_flag, estimate_obj, condition, house_data_flag, seismic_input_mode, task_id, qh_level) values(:id, :province, :city, :county, :calculatedValue, ST_GeometryFromText(:geom, 4490), :faeId, :borderlandFlag, :estimateObj, :condition, :houseDataFlag, :seismicInputMode, :taskId, :qhLevel)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(result);
        namedParameterJdbcTemplate.update(sql, source);
    }

    public List<FxqhAssessPdQhResult> getAssessPdQhList(String taskId){
        return jdbcTemplate.query("select *,ST_AsText(geom) as geomText from fxqh_assess_pd_qh_result where 1=1 and task_id='"+taskId+"'",new BeanPropertyRowMapper<>(FxqhAssessPdQhResult.class));
    }
    public List<FxqhAssessPdQhResult> getAssessPdQhList(FxqhAssessPdQhResult param) {
        StringBuilder sqlBuilder = new StringBuilder("select *,st_astext(geom) geom from fxqh_assess_pd_qh_result where 1=1 ");
        if (ObjectUtils.isNotEmpty(param.getTaskId())) {
            sqlBuilder.append(String.format(" and task_id = '%s' ", param.getTaskId()));
        }
        if (ObjectUtils.isNotEmpty(param.getEstimateObj())) {
            sqlBuilder.append(String.format(" and estimate_obj = '%s' ", param.getEstimateObj()));
        }
        if (ObjectUtils.isNotEmpty(param.getCondition())) {
            sqlBuilder.append(String.format(" and condition = '%s' ", param.getCondition()));
        }
        sqlBuilder.append(" order by province, city, county asc ");
        return jdbcTemplate.query(sqlBuilder.toString(), new BeanPropertyRowMapper<>(FxqhAssessPdQhResult.class));
    }

    public List<QhSingleExportVO> getByParam(QhSingleExportParam param) {
        String sql="select province as provinceName,city as cityName,county as countyName,condition as years," +
                "calculated_value as calculatedValue,qh_level as qhLevel from fxqh_assess_pd_qh_result where 1=1";
        if (PlatformObjectUtils.isNotEmpty(param.getId())){
            sql=sql+" and task_id='"+param.getId()+"'";
        }
        if (PlatformObjectUtils.isNotEmpty(param.getProvince())){
            sql=sql+" and province='"+param.getProvince()+"'";
        }
        if (PlatformObjectUtils.isNotEmpty(param.getCity())){
            sql=sql+" and city='"+param.getCity()+"'";
        }
        if (PlatformObjectUtils.isNotEmpty(param.getCounty())){
            sql=sql+" and county='"+param.getCounty()+"'";
        }
        if (PlatformObjectUtils.isNotEmpty(param.getAssessObj())){
            sql=sql+" and estimate_obj='"+param.getAssessObj()+"'";
        }
        if (PlatformObjectUtils.isNotEmpty(param.getSeismicInputMode())){
            sql=sql+" and seismic_input_mode='"+param.getSeismicInputMode()+"'";
        }
        if (PlatformObjectUtils.isNotEmpty(param.getYear())){
            sql=sql+" and condition='"+param.getYear()+"'";
        }
        return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(QhSingleExportVO.class));
    }
}
